﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace FlyBird
{
    public partial class Form1 : Form
    {
        // 实例化背景类
        private GameBackGround gameBackGround;
        //实例化小鸟类
        private Bird bird;
        //实例化前景类
        private GameFrontGround gameFrontGround;
        //实例化障碍物层
        private GameBarrierLayer gameBarrierLayer;

        private Bitmap buffImg;// 用于缓存绘制的内容

        private bool isStart; //游戏是否开始
        

        public Form1()
        {
            InitializeComponent();
            // 启用双缓冲
            this.DoubleBuffered = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitGame();
            isStart = false;
            Thread thread = new Thread(this.Run);
            thread.IsBackground = true;
            thread.Start();
        }

        public void InitGame()
        {
            gameBackGround = new GameBackGround();
            bird = new Bird();
            gameFrontGround = new GameFrontGround();
            gameBarrierLayer = new GameBarrierLayer();
        }

        public void Run()
        {
            while (true)
            {
                this.Invalidate(); // 请求重绘
                try
                {
                    Thread.Sleep(30);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }
        }
        //重写绘制时间
        protected override void OnPaint(PaintEventArgs e)
        {
            update(e.Graphics);
        }
        //更新画面
        public void update(Graphics g)
        {
            // 绘制游戏内容到缓冲区，先将所有内容绘制到一张图上，在将那张图绘制到屏幕上
            if (buffImg == null)
            {
                buffImg = new Bitmap(StaticValue.FRAM_WIDTH, StaticValue.FRAM_HEIGHT);
            }
            Graphics graphics = Graphics.FromImage(buffImg);
            //游戏开始
            if (isStart)
            {
                //小鸟存活，绘制障碍物小鸟等等。
                if (bird.life)
                {
                    
                    graphics.Clear(Color.Transparent);

                    gameBackGround.draw(graphics);
                    gameFrontGround.draw(graphics);
                    bird.draw(graphics);
                    gameBarrierLayer.draw(graphics, bird);
                    g.DrawImage(buffImg, 0, 0);
                }
                //游戏结束保留一秒的画面，绘制提示文字
                else
                {
                    //string over = "游戏结束";
                    //Brush brush = new SolidBrush(Color.Red); // 使用 SolidBrush 创建 Brush 对象
                    //Font font = new Font("微软雅黑", 40, FontStyle.Bold);
                    g.Clear(Color.Transparent);


                    g.DrawImage(buffImg, 0, 0);
                    //g.DrawString(over, font, brush, new PointF(125, 150));
                    g.DrawImage(StaticValue.GAME_OVER, 225, 200);

                    //new Font("微软雅黑", 20, FontStyle.Bold);
                    //brush = new SolidBrush(Color.Black);
                    //string reset = "按下空格重新开始";
                    g.DrawImage(StaticValue.GAME_AGAIN, 225, 250);
                }
            }
            //游戏未开始，只绘制背景前景。
            else
            {
                graphics.Clear(Color.Transparent);
                gameBackGround.draw(graphics);
                gameFrontGround.draw(graphics);
                graphics.DrawImage(StaticValue.GAME_START, 225, 200);
                graphics.DrawImage(StaticValue.GAME_AGAIN, 225, 270);
                g.DrawImage(buffImg, 0, 0);
            }
        }

        //按下
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            add(e);
        }
        //弹起
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            minu(e);
        }

        //按键
        public void add(KeyEventArgs e)
        {
            switch (e.KeyCode)
            { 
                case Keys.Up:
                    bird.fly(Bird.STATE_UP);
                    if (!isStart)
                    {
                        //将游戏设置成开始
                        isStart = true;
                    }
                    break;
                default:
                    if (bird.life == false)
                    {
                        restart();
                    }
                    if (!isStart)
                    {
                        //将游戏设置成开始
                        isStart = true;
                    }
                    break;
            }
        }

        //抬起
        public void minu(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    bird.fly(Bird.STATE_DOWN);
                    break;
            }
        }

        //重置游戏
        public void restart()
        {
            gameBarrierLayer.restant();
            bird.restartDraw();
        }
    }
}